package com.cz.video.web.controller.website;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import com.cz.video.common.annotation.LegalValidate;
import com.cz.video.common.annotation.Log;
import com.cz.video.common.core.controller.BaseController;
import com.cz.video.common.core.domain.PageQuery;
import com.cz.video.common.core.domain.R;
import com.cz.video.common.core.page.TableDataInfo;
import com.cz.video.common.core.validate.AddGroup;
import com.cz.video.common.core.validate.EditGroup;
import com.cz.video.common.enums.BusinessType;
import com.cz.video.common.utils.ValidatorUtils;
import com.cz.video.website.domain.WebsiteBanner;
import com.cz.video.website.domain.bo.WebsiteBannerBo;
import com.cz.video.website.domain.vo.WebsiteBannerVo;
import com.cz.video.website.service.IWebsiteBannerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author yr
 * @since 2023-10-15
 */
@Tag(name="轮播图管理")
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/website/banner")
public class WebsiteBannerController extends BaseController {
    public final IWebsiteBannerService websiteBannerService;

    /**
     * 添加轮播图
     * @param websiteBanner
     * @return
     */
    @SaCheckPermission("website:banner:add")
    @Operation(summary = "添加轮播图")
    @Log(title = "添加轮播图", businessType = BusinessType.INSERT)
    @PostMapping("")
    @LegalValidate
    public R<Void> add(@RequestBody @Validated(AddGroup.class) WebsiteBanner websiteBanner){
        return toAjax(websiteBannerService.insertBanner(websiteBanner));
    }

    /**
     * 删除轮播图
     * @param id
     * @return
     */
    @SaCheckPermission("website:user:delete")
    @Operation(summary = "删除轮播图")
    @Log(title = "删除轮播图", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Void> delete(@PathVariable int id){
        int result = websiteBannerService.deleteById(id);
        return toAjax(result);
    }

    /**
     * 修改轮播图
     */
    @SaCheckPermission("website:banner:edit")
    @Log(title = "修改轮播图", businessType = BusinessType.UPDATE)
    @PutMapping("")
    public R<Void> edit( @RequestBody WebsiteBannerBo websiteBanner) {
        ValidatorUtils.validate(websiteBanner, EditGroup.class);
        return toAjax(websiteBannerService.updateBanner(websiteBanner));
    }

    /**
     * 获取轮播图信息列表
     */
    @SaIgnore
    @GetMapping("/list")
    public TableDataInfo<WebsiteBannerVo> list(WebsiteBannerBo websiteBanner, PageQuery pageQuery) {
        return websiteBannerService.selectPageBannerList(websiteBanner, pageQuery);
    }
}
